﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MonsterEngine.Interfaces;
using MonsterEngine.Types;

namespace MonsterEngine.Managers
{
    public class DamageManager
    {
        private Random _rndDamage = new Random();

        public int DamageCounter(int damageAmount, DamageType damageType)
        {
            int modDamage = GetDamageWithModifiers(damageAmount, damageType);
            PlayerManager.Player1.Target.HitPoints -= modDamage;
            Console.WriteLine("Damage amount to player: " + PlayerManager.Player1.Target.Name + " was hit for " + modDamage);
            return modDamage;
        }

        private int GetDamageWithModifiers(int initialDamage, DamageType damageType)
        {
            int defense = 0;
            int totalDamage = 0;
            switch (damageType)
            {
                case DamageType.Magical:
                    defense = PlayerManager.Player2.MagicalDefense;
                    initialDamage += PlayerManager.Player1.SpellPower;
                    Console.WriteLine("{0}'s magical defense {1}", PlayerManager.Player2.Name, PlayerManager.Player2.MagicalDefense);
                    break;
                case DamageType.Physical:
                    defense = PlayerManager.Player2.PhysicalDefense;
                    initialDamage += PlayerManager.Player1.AttackPower;
                    Console.WriteLine("{0}'s physical defense {1}", PlayerManager.Player2.Name, PlayerManager.Player2.PhysicalDefense);
                    break;
                case DamageType.None:
                    break;
            }

            // Player defense was higher than total damage dealt
            if (initialDamage < defense)
            {
                Console.WriteLine("{0} has defended the attack", PlayerManager.Player2.Name);
                return 1;
            }

            // Damage greater than defense
            if (initialDamage > defense)
            {
                if (defense < 0)  // Player had negative defense
                {
                    totalDamage = initialDamage + ((defense) * -1); // Crushing blow
                    Console.WriteLine("Initial damage {0} augmented to {1}", initialDamage, totalDamage);
                    Console.WriteLine("It's called defense...brah...");
                }
                else
                {
                    totalDamage = initialDamage - defense;
                    Console.WriteLine("Initial damage {0} defended to {1}", initialDamage, totalDamage);
                }
            }

            return totalDamage;
        }

        public int DoRandomDamage()
        {
            int damageAmount = _rndDamage.Next(PlayerManager.Player1.MinDamage, PlayerManager.Player1.MaxDamage);
            return damageAmount;
        }
    }
}
